﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using dotsoftRAID_GUI.Properties;
using System.IO;
using System.Runtime.InteropServices;
using dotsoftRAID_API;

namespace dotsoftRAID_GUI
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private const int MAX_PATH = 256;

        [DllImport("mscoree.dll",
        CharSet = CharSet.Unicode,
        ExactSpelling = true)]
        public static extern int GetCORSystemDirectory(
        StringBuilder buf,
        int cchBuf,
        ref int cchRequired);

        /// <summary>
        /// Function that return .net framework installation path.
        /// This code is from: http://forums.asp.net/t/1448679.aspx
        /// </summary>
        public string GetNetFrameworkDirectory()
        {
            StringBuilder buf = new StringBuilder(
            MAX_PATH, MAX_PATH);
            int cch = MAX_PATH;
            int hr = GetCORSystemDirectory(
            buf, MAX_PATH, ref cch);
            if (hr < 0) Marshal.ThrowExceptionForHR(hr);
            return buf.ToString();
        }

        private void homepageBtn_Click(object sender, EventArgs e)
        {
            using (Process homepage = new Process())
            {
                homepage.StartInfo.FileName = "http://dotsoftraid.codeplex.com/";
                homepage.Start();
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (!Settings.Default.ServiceInstalled)
            {
                //Install service!
                using (Process serviceInstaller = new Process())
                {
                    serviceInstaller.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "service-install.bat");
                    serviceInstaller.StartInfo.Arguments = Path.Combine(GetNetFrameworkDirectory(), "InstallUtil.exe");
                    serviceInstaller.Start();
                }

                Settings.Default.ServiceInstalled = true;
            }
        }

        private void raidTypeComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            DriveSelect driveSelect = new DriveSelect();
            driveSelect.ShowDialog();
        }

        private void deinstallRAIDSystemBtn_Click(object sender, EventArgs e)
        {
            
        }

        private void installRAIDSystemBtn_Click(object sender, EventArgs e)
        {
            List<string> devices = new List<string>();

            foreach (string device in raidDevices.Items)
            {
                devices.Add(device);
            }

            API.InstallRAIDSystem(raidTypeComboBox.SelectedItem.ToString().Split(new char[]{' '})[1], raidSourceDevice.Items[0].ToString(), devices.ToArray());
        }
    }
}
